package com.ruoyi.web.controller.guarantee;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.guarantee.domain.BAgreement;
import com.ruoyi.guarantee.domain.BAgreementCheck;
import com.ruoyi.guarantee.domain.BBidding;
import com.ruoyi.guarantee.domain.BBiddingCheck;
import com.ruoyi.guarantee.service.IBAgreementCheckService;
import com.ruoyi.guarantee.service.IBAgreementService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年11月07日 18:37
 ** description&desc：
 *********************************************************************************/
@RestController
@RequestMapping("/guarantee/check")
public class BAgreementCheckController extends BaseController {
    @Autowired
    private IBAgreementService bAgreementService;
    @Autowired
    private IBAgreementCheckService agreementCheckService;

    // //状态 0 未提交  2提交主管审核  4 业务总审核（主管提交） 6财务确认收钱 8运营确认送审银行（不进行下一步）10运营上传保函提交， 12财务核算成本 14 完成。-1 驳回
    /**
     * 业务主管待审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/yyzgList")
    public TableDataInfo yyzgList(BAgreement bAgreement)
    {
        bAgreement.setStatus(2L);//业务员提交的
        startPage();
        List<BAgreement> list = bAgreementService.selectyyzgBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 审核保存
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @PostMapping("/yyzgCheck")
    public AjaxResult yyzgCheck(@RequestBody BAgreement bAgreement)
    {
        BAgreement bAgreementOld = bAgreementService.selectBAgreementById(bAgreement.getId());
//        BAgreement bAgreementNew = new BAgreement();
//        bAgreementNew.setId(bAgreement.getId());
//        bAgreementNew.setStatus(bAgreement.getStatus());
        //查询订单
        if (bAgreement.getStatus().longValue()==-1){//驳回
            bAgreement.setRecallMind(bAgreement.getCheckMind());
            bAgreement.setRecallTime(new Date());
            bAgreement.setRejectUserName(getUsername());
        }
        bAgreement.setUpdateTime(new Date());
        bAgreementService.updateBAgreement(bAgreement);
        BAgreementCheck agreementCheck =
                new BAgreementCheck();
        agreementCheck.setAgreementId(bAgreement.getId());
        agreementCheck.setCheckMind(bAgreement.getCheckMind());
        agreementCheck.setCheckStatus(bAgreement.getStatus());
        agreementCheck.setOldStatus(bAgreementOld.getStatus());
        agreementCheck.setUserName(getUsername());
        agreementCheck.setUserId(getUserId());
        agreementCheck.setCreateTime(new Date());
        agreementCheck.setUserName(SecurityUtils.getUsername());
        return toAjax(agreementCheckService.insertBAgreementCheck(agreementCheck));
    }

    /**
     * 业务总待审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/yyzList")
    public TableDataInfo yyzList(BAgreement bAgreement)
    {
        bAgreement.setStatus(4L);//只审核业务主管订单
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 财务待确认收到钱审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/cwqList")
    public TableDataInfo cwqList(BAgreement bAgreement)
    {
        bAgreement.setStatus(6L);
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 运营待确认审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/yyBList")
    public TableDataInfo yyBList(BAgreement bAgreement)
    {
        bAgreement.setStatus(8L);
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 运营待确认审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/yyHList")
    public TableDataInfo yyHList(BAgreement bAgreement)
    {
        bAgreement.setStatus(10L);
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 财务待确认公司收益成本审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:agreement:list')")
    @GetMapping("/cwCList")
    public TableDataInfo cwCList(BAgreement bAgreement)
    {
        bAgreement.setStatus(12L);
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 提交投标保函
     */
    @Log(title = "投标保函", businessType = BusinessType.INSERT)
    @PostMapping("/yybSave/{id}")
    public AjaxResult yybSave(@PathVariable Long id){
        BAgreement bAgreement = bAgreementService.selectBAgreementById(id);
        bAgreement.setStatus(10L);
        bAgreement.setUpdateBy(getUsername());
        bAgreement.setUpdateTime(new Date());
        return toAjax(bAgreementService.updateBAgreement(bAgreement));
    }

}
